package com.LachesismStorm.lachesism.commonTools;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;

/**
 * @Author lachesism
 * @Date 2024-12-26 20:09
 **/
public class replaceTextInImage {

    public static class TextInfo {
        String text;
        int x;
        int y;

        public TextInfo(String text, int x, int y) {
            this.text = text;
            this.x = x;
            this.y = y;
        }
    }

    public static void replaceTextInImage(String imagePath, TextInfo[] texts, String outputPath,String overlayImagePath) throws IOException {
        // 读取图片
        BufferedImage image = ImageIO.read(new File(imagePath));

        // 获取图片的Graphics对象
        Graphics2D g2d = image.createGraphics();

        // 开启抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 设置字体、颜色等
        g2d.setFont(new Font("Songti SC", Font.BOLD, 77));  // 可以根据需要调整字体
        g2d.setColor(Color.decode("#364741"));  // 设置文字颜色为 #364741

        // 创建一个新的BufferedImage来绘制文本
        BufferedImage textLayer = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2dText = textLayer.createGraphics();

        // 开启抗锯齿
        g2dText.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2dText.setFont(new Font("Songti SC", Font.BOLD, 77));  // 可以根据需要调整字体
        g2dText.setColor(Color.decode("#364741"));  // 设置文字颜色为 #364741

        // 遍历传入的文本信息数组，并绘制每个文本
        for (TextInfo textInfo : texts) {
            g2dText.drawString(textInfo.text, textInfo.x, textInfo.y);
        }

        g2dText.dispose();

        // 对文本层应用高斯模糊
        BufferedImage blurredTextLayer = applyGaussianBlur(textLayer);

        // 将模糊后的文本层绘制到原图像上
        g2d.drawImage(blurredTextLayer, 0, 0, null);
        // 读取要添加的图片（无需调整大小）
        BufferedImage overlayImage = ImageIO.read(new File(overlayImagePath));
        // 你可以根据需要调整图片的绘制位置，当前是放置在(100, 100)位置
        g2d.drawImage(overlayImage, 166, 160, null);
        // 将图片直接绘制到原图上
        g2d.dispose();

        // 保存修改后的图片
        ImageIO.write(image, "png", new File(outputPath));
    }

    // 应用高斯模糊
    public static BufferedImage applyGaussianBlur(BufferedImage image) {
        float[] matrix = {
                1f / 16f, 2f / 16f, 1f / 16f,
                2f / 16f, 4f / 16f, 2f / 16f,
                1f / 16f, 2f / 16f, 1f / 16f
        };
        Kernel kernel = new Kernel(3, 3, matrix);  // 使用 3x3 核心
        ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        return convolveOp.filter(image, null);  // 对图像应用模糊
    }

    // 使用示例
    public static void main(String[] args) throws IOException {
        TextInfo[] texts = {
                new TextInfo("李金玉", 350, 1200),
                new TextInfo("女", 1000, 1200),
                new TextInfo("1996.03.29", 530, 1500),
                new TextInfo("654222199603290225", 510, 1800),
                new TextInfo("汽车修理电工", 2200, 375),
                new TextInfo("高级", 2250, 680),
                new TextInfo("20241115241", 2300, 990),
                new TextInfo("2024", 2180, 1827),
                new TextInfo("11", 2450, 1827),
                new TextInfo("24", 2650, 1827),
        };
        replaceTextInImage(
                "/Users/yuno/Downloads/e60.png",
                texts,
                "/Users/yuno/Downloads/60a.png",
                "/Users/yuno/Downloads/12301525138.jpg"
        );
    }
}
//524 x 707